import io from 'socket.io-client'
import axios from 'axios'
import { log } from 'util';
const socket=io('ws://localhost:9093')
//获取聊天列表
const MSG_LIST='MSG_LIST'
//读取信息
const MSG_RECV='MSG_RECV'
//标识已读
const MSG_READ='MSG_READ'
//标识接收消息
const RECV_MSG_FLAG='RECV_MSG_FLAG'
const initState={
    chatmsg:[],
    users:{},
    unread:0,
    recvMsgFlag:true

}
//Store 收到 Action 以后，必须给出一个新的 State，这样 View 才会发生变化。这种 State 的计算过程就叫做 Reducer。
//Reducer 是一个函数，它接受 Action 和当前 State 作为参数，返回一个新的 State。
export function chat(state=initState,action) {
    // console.log(action);
    
    switch (action.type) {
        case MSG_LIST:
            return {...state,users:action.payload.users,chatmsg:action.payload.msgs,unread:action.payload.msgs.filter(v=>!v.read&&v.to==action.payload.userid).length,
                recvMsgFlag:action.payload.recvMsgFlag }
        case MSG_RECV:
            const n=action.payload.to==action.userid?1:0
            //接收到消息 to是自己的时候 unread++
            return {...state,chatmsg:[...state.chatmsg,action.payload],unread:state.unread+n}
        case MSG_READ:
        const {from,num}=action.payload;
                
            return {...state,chatmsg:state.chatmsg.map(v=>({...v,read:from==v.from?true:v.read})),unread:state.unread-num}
        case RECV_MSG_FLAG:
            return{...state,recvMsgFlag:action.payload.recvMsgFlag} 
        default:
            return state
    }
}
// Action Creator 生成action
function msglist(msgs,users,userid) {
    
    return {type :MSG_LIST,payload:{msgs,users,userid,recvMsgFlag:false}}
}
function recvmsg(msgs,userid) {
    return {userid,type:MSG_RECV,payload:msgs}
}
function changerecvmsgflag(recvMsgFlag){
    return {type:RECV_MSG_FLAG,payload:{recvMsgFlag}}
}
function msgRead({from,userid,num}) {
    return {type:MSG_READ,payload:{from,userid,num}}
}
//store.dispatch 是发出dispatch的唯一办法
export function getMsgList(userTo) {
    userTo=userTo?userTo:{}
    // console.log(userTo)
    return (dispatch,getState)=>{
        axios.post('/user/getmsglist',userTo)
        .then(data=>{
            if(data.status==200&&data.data.code==0){
                const userid=getState().user._id;
                dispatch(msglist(data.data.msgs,data.data.users,userid))
            }
        })
    }
}
export function sendMsg({from,to,msg,avatar}) {
    return dispatch=>{
        socket.emit('sendMsg',{from,to,msg,avatar})
    }
  }
export function recvMsg() {
    return (dispatch,getState)=>{
        
        socket.on('recvMsg',(data)=>{
            const userid=getState().user._id;
            dispatch(recvmsg(data,userid))
        })
    }
  }
export function changeRecvMsgFlag(flag){
    return (dispatch)=>{
        dispatch(changerecvmsgflag(flag))
    }
}

export function readMsg(from) {
    return (dispatch,getState)=>{
        axios.post('/user/readmsg',{from})
        .then(res=>{
            const userid=getState().user._id;
            if(res.status==200&&res.data.code==0){
                dispatch(msgRead({userid,from,num:res.data.num}))
            }
        })
        
    }
}